<?php

namespace App\Http\Controllers\Project\Api;

use App\Http\Controllers\Project\Controller;
use App\Models\ExternalContact;
use App\Models\Project\ProjectType;
use App\Models\User;
use App\Traits\Admin\SuccessOrFailure;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;


class ContractApiController extends Controller
{
    use SuccessOrFailure;

    /**
     * 根据项目类型获取专业人
     *
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     * @Author: hql <858566651@qq.com>
     * @DateTime: 2021/12/19 20:31
     */
    public function professionalDirectors(Request $request)
    {
        $project_type_id = $request->input('project_type_id') ?? 1;
        $default_professional_uid = 0;
        $project_type = ProjectType::find($project_type_id);
        if ($project_type) {
            $default_professional_uid = $project_type->default_professional_uid;
        }

        $professional_directors = get_user_by_role(['professional_director']);

        $users = User::query()->leftJoin('user_project_type', 'user_project_type.user_id', 'users.id')
            ->where('user_project_type.project_type_id', $project_type_id)
            ->whereIn('users.id', $professional_directors->pluck('id')->toArray())
            ->select(['users.id', 'users.name'])->get()->values()->toArray();
        return response()->json(['code' => 200, 'data' => ['default_professional_uid' => $default_professional_uid, 'users' => $users], 'message' => 'success'], 200);
    }

    /**
     * 获取外部联系人
     * @return \Illuminate\Http\JsonResponse
     * @Author: hql <858566651@qq.com>
     * @DateTime: 2021/12/19 20:50
     */
    public function externalContacts(Request $request)
    {
        $category = $request->input('category');
        $project_source = $request->input('project_source');

        $contacts = ExternalContact::query()
            ->where('category', $category)
            ->where('project_source', $project_source)
            ->where('related_uid', Auth::id())
            ->select(['id', 'unit', 'username', 'mobile'])
            ->get()
            ->values()
            ->toArray();
        return response()->json(['code' => 200, 'data' => $contacts, 'message' => 'success'], 200);
    }

}


